import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerPostExperienceApi } from '#/api/per/postExperience';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

import { getFileUrl } from '#/utils/fileUtils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },

    {
      fieldName: 'postUnit',
      label: '任职单位',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入任职单位',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'postType',
      label: '岗位类别',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_POST_TYPE),
        placeholder: '请选择岗位类别',
      },
    },
    {
      fieldName: 'postLevel',
      label: '岗位等级',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_POST_LEVEL),
        placeholder: '请输入岗位等级',
      },
    },
    {
      fieldName: 'postFile',
      label: '任职文件',
      component: 'FileUpload',
      componentProps: {
        params: {
          fileCategoriesCode: '',
          objectId: 'postFile',
          objectType: '任职文件',
        },
      },
    },

    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '任职单位',
      field: 'postUnit',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '开始时间',
      field: 'startDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '结束时间',
      field: 'endDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '岗位类别',
      field: 'postTypeName',
    },
    {
      label: '岗位等级',
      field: 'postLevel',
    },
    {
      label: '任职文件',
      field: 'postFile',
      render: (val) => {
        return data?.postFile
          ? h(
              'a',
              { href: getFileUrl(val, true), target: '_blank' },
              () => '点击查看',
            )
          : '未上传';
      },
    },
    {
      label: '是否有效（1：有效，0：无效）',
      field: 'statusName',
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'postUnit',
      label: '任职单位',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入任职单位',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'postType',
      label: '岗位类别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PER_POST_TYPE),
        placeholder: '请选择岗位类别',
      },
    },
    {
      fieldName: 'postLevel',
      label: '岗位等级',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PER_POST_LEVEL),
        placeholder: '请输入岗位等级',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerPostExperienceApi.PostExperience>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },

    {
      field: 'postUnit',
      title: '任职单位',
      minWidth: 120,
    },
    {
      field: 'startDate',
      title: '开始时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'endDate',
      title: '结束时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'postType',
      title: '岗位类别',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_POST_TYPE },
      },
    },
    {
      field: 'postLevel',
      title: '岗位等级',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_POST_LEVEL },
      },
    },

    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
